﻿Ext.ns('Ext.ux');

Ext.ux.AdvHtmlEditor = Ext.extend(Ext.form.HtmlEditor, {
	addImage : function() {
		var editor = this;
		var form = new Ext.FormPanel({
			labelWidth : 55,
			frame : true,
			bodyStyle : 'padding:5px 5px 0',
			border : false,
			fileUpload : true,
			autoHeight : true,
			items : [{
						xtype : 'fileuploadfield',
						fieldLabel : '选择图片',
						buttonText : '浏览...',
						name : 'userfile',
						allowBlank : false,
						emptyText : '请选择本地图片(jpg、bmp、gif、png)',
						anchor : '98%'
					}],
			buttons : [{
				text : '上传',
				type : 'submit',
				handler : function() {
					var furl = form.form.findField('userfile').getValue();
					if (Ext.isEmpty(furl)) return;
					var type = furl.substring(furl.length - 3).toLowerCase();
					if (type != 'jpg' && type != 'bmp' && type != 'gif' && type != 'png') {
						alerterr('仅支持jpg、bmp、gif、png格式的图片');
						return;
					}
					ajaxform({
								form : form,
								url : url.uploadFile,
								waitmsg : '正在上传......',
								waitTitle : '请等待',
								success : function(result) {
									editor.relayCmd('InsertImage', 'UploadFile/' + result.filename);
									win.close();
								}
							});
				}
			}, {
				text : '关闭',
				handler : function() {
					win.close(this);
				}
			}]
		})
		var win = new Ext.Window({
					title : '上传本地图片',
					width : 400,
					autoHeight : true,
					modal : true,
					border : false,
					iconCls : 'uploadpic-icon',
					layout : 'fit',
					items : form
				});
		win.show();
	},
	linkImage : function() {
		var furl = prompt('请输入远程图片地址', this.defaultLinkValue);
		if (Ext.isEmpty(furl) || furl == this.defaultLinkValue) return;
		var type = furl.substring(furl.length - 3).toLowerCase();
		if (type != 'jpg' && type != 'bmp' && type != 'gif' && type != 'png') {
			alerterr('仅支持jpg、bmp、gif、png格式的图片');
			return;
		}
		this.relayCmd('InsertImage', furl);
	},
	addFile : function() {
		var editor = this;
		var form = new Ext.FormPanel({
					labelWidth : 55,
					frame : true,
					bodyStyle : 'padding:5px 5px 0',
					border : false,
					fileUpload : true,
					autoHeight : true,
					items : [{
								xtype : 'fileuploadfield',
								fieldLabel : '选择文档',
								buttonText : '浏览...',
								name : 'userfile',
								allowBlank : false,
								emptyText : '请选择本地文档(doc、docx、xls、pdf等)',
								anchor : '98%'
							}],
					buttons : [{
						text : '上传',
						type : 'submit',
						handler : function() {
							var furl = form.form.findField('userfile').getValue();
							if (Ext.isEmpty(furl)) return;
							var type = furl.substring(furl.length - 4).toLowerCase();
							if (type != '.doc' && type != '.docx' && type != '.xls'
									&& type != 'xlsx' && type != '.ppt' && type != 'pptx'
									&& type != '.pdf') {
								alerterr('仅支持doc、docx、xls、xlsx、ppt、pptx、pdf格式的文档');
								return;
							}
							var filename = furl.substring(furl.lastIndexOf("\\") + 1);
							ajaxform({
										form : form,
										url : url.uploadFile,
										waitmsg : '正在上传......',
										waitTitle : '请等待',
										success : function(result) {
											var htm = String
													.format(
															'<a target="_blank" href="UploadFile/{0}">{1}</a>',
															result.filename, filename)
											editor.insertAtCursor(htm);
											win.close();
										}
									});
						}
					}, {
						text : '关闭',
						handler : function() {
							win.close(this);
						}
					}]
				})
		var win = new Ext.Window({
					title : '上传本地文档',
					width : 400,
					autoHeight : true,
					modal : true,
					border : false,
					iconCls : 'uploadfile-icon',
					layout : 'fit',
					items : form
				});
		win.show();
	},
	addflash : function() {
		var furl = prompt('请输入远程Flash地址', this.defaultLinkValue);
		if (Ext.isEmpty(furl) || furl == this.defaultLinkValue) return;
		var htm = String
				.format(
						'<embed style="width:300px;height:200px" type="application/x-shockwave-flash" quality="high" src="{0}"></embed>',
						furl)
		this.insertAtCursor(htm);
	},
	addfilm : function() {
		var furl = prompt('请输入远程多媒体地址', this.defaultLinkValue);
		if (Ext.isEmpty(furl) || furl == this.defaultLinkValue) return;

		var regImg = new RegExp(/\.(mp3|wav|wma|wmv|avi|mpg|asf|rm|rmvb)$/);
		var regrm = new RegExp(/\.(rm|rmvb)$/);
		if (!furl.match(regImg)) {
			alerterr('仅支持以下格式媒体：mp3,wav,wma,wmv,avi,mpg,asf,rm,rmvb');
			return;
		}
		var ftype = furl.match(regrm) ? 'audio/x-pn-realaudio-plugin' : 'video/x-ms-asf-plugin';
		var htm = String
				.format(
						'<embed style="width:300px;height:200px" type="{1}" src="{0}" loop="true" autostart="true"></embed>',
						furl, ftype)
		this.insertAtCursor(htm);
	},
	createToolbar : function(editor) {
		Ext.ux.AdvHtmlEditor.superclass.createToolbar.call(this, editor);
		this.tb.insertButton(17, {
					cls : "x-btn-icon",
					iconCls : "uploadpic-icon",
					handler : this.addImage,
					tooltip : "上传图片",
					scope : this
				});
		this.tb.insertButton(18, {
					cls : "x-btn-icon",
					iconCls : "linkpic-icon",
					handler : this.linkImage,
					tooltip : "添加网络图片",
					scope : this
				});
		this.tb.insertButton(19, {
					cls : "x-btn-icon",
					iconCls : 'uploadfile-icon',
					handler : this.addFile,
					tooltip : "上传文档",
					scope : this
				});
		this.tb.insertButton(20, {
					cls : "x-btn-icon",
					iconCls : 'swf-icon',
					handler : this.addflash,
					tooltip : "添加网络Flash",
					scope : this
				});
		this.tb.insertButton(21, {
					cls : "x-btn-icon",
					iconCls : 'film-icon',
					handler : this.addfilm,
					tooltip : "添加网络多媒体",
					scope : this
				});
		this.tb.insertButton(22, {// 插入层的扩展
			cls : "x-btn-icon",
			iconCls : 'div-icon',
			handler : function() {
				var htm = '<div style="background-color:rgb(250,250,251);border:1px solid rgb(174,203,231)">在此添加内容<br></div><br>';
				this.insertAtCursor(htm);
			},
			tooltip : "添加div层",
			scope : this
		});
		this.tb.insertButton(23, { // 插入横线的扩展
			cls : "x-btn-icon",
			iconCls : 'hr-icon',
			handler : function() {
				var htm = '<hr>';
				this.insertAtCursor(htm);
			},
			tooltip : "添加横线",
			scope : this
		});
	}
});
Ext.reg('advhtmleditor', Ext.ux.AdvHtmlEditor);
